<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\facade\Builder;
use think\facade\View;
use think\response\Json;

//该文件为自动生成，请按需修改
class SeckillProduct extends Base
{
    //所有定义变量均为演示，请根据实际需求更改

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\SeckillProduct';

    //关联模型
    protected array $hasOne = ['product'];


    public function index($id): string
    {
        //构建表格数据
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'title' => '全选', 'fixed' => 'left'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '商品名称', 'modify' => '2,input','templet' => '<span>{{d.product.title}}</span>'])
            ->addColumn(['field' => 'stock', 'title' => '库存', 'modify' => '2,input', 'templet' => '#sort_stock', 'width' => 120])
            ->addColumn(['field' => 'num', 'title' => '限购', 'modify' => '2,input', 'templet' => '#sort_num', 'width' => 120])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 200])
            ->addRightButtons(['edit' => ['size' => '1000,1000'], 'delete'])
            ->addTopButtons(['add' => 
                [
                'size' => '1000,1000',
                'href'   => url('seckillProduct/add', ['id'=>$id])->build(),
                'event' => '_openNewPage'
                ], 'deleteAll'])
            ->addSearch([
                ['title', '文章标题搜索', 'input', '', '', '', 'like']
            ])
            ->addSortTool([
                ['name' => 'stock', 'pk' => $this->pk, 'url' => url('update')->build()],
                ['name' => 'num', 'pk' => $this->pk, 'url' => url('update')->build()]
            ])
            ->addExtraMap(['seckill_id'=>$id])
            ->fetch();
    }

    public function add($tpl = 'seckillproduct/edit')
    {
        View::assign('seckill_id', $this->request->get('id'));
        return parent::add($tpl);
    }

    public function edit($id = '', $tpl = 'seckillproduct/edit', $info = '')
    {
        $info = $this->model::find($id);
        return parent::edit($id, $tpl, $info);
    }

    public function saveSeckill(): Json
    {
        return json($this->model::saveProduct($this->request->post())); // TODO: Change the autogenerated stub
    }

    
    public function getProduct()
    {
        $param = $this->request->param();
        if(isset($param['ids'])){
            $Product = \app\common\model\Product::where('id', $param['ids'])->where(['is_del'=>0,'status'=>1])->field('id,title')->find();
        }else{
            $Product = \app\common\model\Product::whereLike('title', '%' . $param['val'] . '%')->where(['is_del'=>0,'status'=>1])->field('id,title')->select();
        }
        return json(['code' => 0, 'data' => $Product]);
       
    }
}
